/**
 * 
 */
package com.feib.stms.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;



import com.feib.stms.model.UploadFileTodoList;
import com.feib.stms.model.User;
import com.feib.stms.pagination.PaginatedList;
import com.feib.stms.service.UploadFileTodoListService;
import com.feib.stms.util.UserUtil;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.DontBind;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.integration.spring.SpringBean;

/**
 * @title (#)Stms9002ActionBean.java<br>
 * @description     待辦事項查詢 <br>
 * @author Jeff Gu
 *
 */

public class Stms9002ActionBean extends BaseActionBean{

	/**
	 *連結需修正
	 *分公司權限
	 */

	private static final long serialVersionUID = 253593380102915931L;


	/* 基本頁面 此功能不需轉頁 只需要顯示個人的待辦事項 */
	private static final String DEFAULT_JSP = "/WEB-INF/views/stms9002/stms9002.jsp";


	private static List<String> SORT_KEYS = new ArrayList<String>();
	static{
		SORT_KEYS.add("parentMenuItemActionName".toLowerCase());//待辦模組
		SORT_KEYS.add("actionName".toLowerCase());//待辦事項 	 	
		SORT_KEYS.add("count".toLowerCase());// 檔案數 (筆數)
	}
	/*使用SORT_KEYS作key值取得對應的資料庫欄位名稱*/
	private static Map<String,String> SORT_FIELD =  new HashMap<String, String>();
	static{
		SORT_FIELD.put("parentMenuItemActionName".toLowerCase(), "MO.ACTION_NAME");
		SORT_FIELD.put("actionName".toLowerCase(), "MI.ACTION_NAME");
		SORT_FIELD.put("count".toLowerCase(), "count");

	}



	/**
	 * 使用者
	 * */
	private User user;

	/**
	 * 查詢結果(多筆)
	 */
	private PaginatedList<UploadFileTodoList> queryResult;



	private String message;



	@SpringBean("uploadFileTodoListService")
	private UploadFileTodoListService uploadFileTodoListService;




	@DontBind
	@DefaultHandler
	public Resolution doDefault()
	{
		logger.debug("Enter doDefault mehtod.");

		return doQuery(); 


	}

	/**
	 * 排序用查詢
	 * @return
	 */
	public Resolution doQuery()
	{
		logger.debug("Enter doQuery mehtod.");

		user =  UserUtil.getUser();
		String sortCriterion = "parentMenuItemName";
		int sortDirection = 1;

		/* 設定換頁、排序等條件*/
		PaginatedList<UploadFileTodoList> qryCondition = new PaginatedList<UploadFileTodoList>();
		if (null != queryResult)
		{
			sortCriterion = queryResult.getSortCriterion();
			sortDirection = queryResult.getSortDirection();
			qryCondition.setPageNumber(queryResult.getPageNumber());
			qryCondition.setPageSize(queryResult.getPageSize());
			String StringSortCriterion=SORT_FIELD.get(sortCriterion.toLowerCase());
			qryCondition.setSortCriterion(StringSortCriterion);
			qryCondition.setSortDirection(sortDirection);
		}

		/*沒有設定排序就以第一欄作排序條件*/
		if (null == queryResult || StringUtils.isEmpty(queryResult.getSortCriterion()) || !SORT_KEYS.contains(queryResult.getSortCriterion().toLowerCase()))
		{
			qryCondition.setSortCriterion(SORT_FIELD.get(SORT_KEYS.get(0)));
		}

		this.queryResult=(uploadFileTodoListService.findUploadFileTodoList(user, qryCondition));

		if (null == this.queryResult)
			this.queryResult = qryCondition;


		queryResult.setSortCriterion(sortCriterion);
		queryResult.setSortDirection(sortDirection);


		/* 查無資料狀態 改為不跳視窗 顯示在畫面 */ 
		//  if (CollectionUtils.isEmpty(this.queryResult.getPaginatedList()))
		//  	this.message = "查無資料！";


		return new ForwardResolution(DEFAULT_JSP); 
	}


	/**
	 * @param queryResult 的設定的 queryResult
	 */
	public void setQueryResult(PaginatedList<UploadFileTodoList> queryResult) {
		this.queryResult = queryResult;
	}




	/**
	 * @return queryResult
	 */
	public PaginatedList<UploadFileTodoList> getQueryResult() {
		return queryResult;
	}




	/**
	 * @param message 的設定的 message
	 */
	public void setMessage(String message) {
		this.message = message;
	}




	/**
	 * @return message
	 */
	public String getMessage() {
		return message;
	}




}
